//
//  SettingViewController.m
//  liangyi
//
//  Created by 张水 on 16/12/15.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "SettingViewController.h"
#import "settingModel.h"
#import "settingCell.h"
#import "ColorButton.h"
#import "ChangePasswordViewController.h"
@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,UIActionSheetDelegate>

@property(nonatomic,strong) NSArray  *dataArr;

@property(nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) UIButton *ExitBtn;

@property(nonatomic,strong) UIAlertView *alertView;

@end

@implementation SettingViewController

static NSString *setting = @"settingCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
}
-(NSArray *)dataArr {
    
    if (!_dataArr){
        NSString *path = [[NSBundle mainBundle]pathForResource:@"Setting.plist" ofType:nil];
        NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *mutable = [NSMutableArray array];
        for (NSDictionary *dict in tempArr) {
            settingModel *setModel = [settingModel cellWithDict:dict];
            [mutable addObject:setModel];
        }
        _dataArr = mutable;
    }
    return  _dataArr;
    
}

-(void)initUI {
    
    self.title = @"设置";
    self.view.backgroundColor = [UIColor ColorWithHex:@"#ECF1F2"];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [CommonMethod hiddleExtendCellFromTableview:_tableView];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[settingCell class] forCellReuseIdentifier:setting];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@264);
    }];
    
    
    NSMutableArray *colorArray = [@[[UIColor ColorWithHex:@"#e65067"],[UIColor ColorWithHex:@"#f02d3c"]] mutableCopy];
    self.ExitBtn = [[ColorButton alloc]initWithFrame:CGRectMake(80, 360, 300, 50) FromColorArray:colorArray ByGradientType:topToBottom];
    self.ExitBtn.layer.cornerRadius = 10;
    self.ExitBtn.layer.masksToBounds = YES;
    [self.ExitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.ExitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.ExitBtn addTarget:self action:@selector(logOutDidClick) forControlEvents:UIControlEventTouchUpInside];
    self.ExitBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view  addSubview:self.ExitBtn];
    [self.ExitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom).offset(60);
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.height.equalTo(@50);
    }];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    settingCell *setingCell = [tableView dequeueReusableCellWithIdentifier:setting];
    setingCell.selectionStyle = UITableViewCellSeparatorStyleNone;
    if (!setingCell) {
        setingCell = [setingCell initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:setting];
    }
    setingCell.setModel = self.dataArr[indexPath.row];
    
    return setingCell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  50;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0){
        [self.navigationController pushViewController:[ChangePasswordViewController new] animated:YES];
    }
    if (indexPath.row == 1){
        
    }
    if (indexPath.row == 2){
        CGFloat size = [[SDImageCache sharedImageCache] getSize]/1024/1024;
        NSString *sizeString = [NSString stringWithFormat:@"缓存为%.2fM\n是否清除",size];
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:sizeString message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:nil];
        UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[SDImageCache sharedImageCache] clearDisk];
            [[SDImageCache sharedImageCache] clearMemory];
        }];
        [alertVC addAction:cancel];
        [alertVC addAction:done];
        [self presentViewController:alertVC  animated:YES completion:nil];
       
    }
    if (indexPath.row == 3){
        
    }
    
}
-(void)logOutDidClick{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"是否退出" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:nil];
    UIAlertAction *done = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [Sp_MemberStorage deleteAllMembers];
        [UserDefaultsUtlis removeObjectForKey:@"userid"];
        [JPUSHService setAlias:@"" callbackSelector:@selector(alias) object:self];//tagsAliasCallback:tags:alias:
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:done];
    [self presentViewController:alertVC  animated:YES completion:nil];
    
    
}

-(void)alias{
    
}
 
@end

